package com.laolang.utils.configfileparse;


import com.laolang.utils.configfileparse.annatation.ConfigurationLocation;
import com.laolang.utils.configfileparse.util.PropertiesFactory;

import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;

public class PropertiesUtils {

    public static Object getConfigClass(Class<?> clazz) {

        Object object = null;
        try {
            object = clazz.newInstance();

            // 获取 clazz 类标记的所有注解
            Annotation[] annotations = clazz.getAnnotations();

            for (Annotation annotation : annotations) {
                // 如果标记了 ConfigurationLocation 注解
                if (clazz.isAnnotationPresent(ConfigurationLocation.class)) {
                    ConfigurationLocation location = (ConfigurationLocation) annotation;
                    Properties properties = PropertiesFactory.getProperties(location.location());

                    // 得到 clazz 所有申明的字段
                    Field[] fields = clazz.getDeclaredFields();
                    for (Field field : fields) {
                        String value;
                        if (null != location.prefix() && !"".equals(location.prefix())) {
                            value = properties.getProperty(location.prefix() + "." + field.getName());
                        } else {
                            value = properties.getProperty(field.getName());
                        }

                        PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                        // 获得写方法
                        Method method = pd.getWriteMethod();
                        method.invoke(object, getValue(field, value));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return object;
    }

    /**
     * 根据属性类型得到正确的属性值
     *
     * @param field 字段
     * @param value properties文件中的属性值
     * @return 对应的 Object 对象
     */
    private static Object getValue(Field field, String value) {
        Object val = value;

        String type = String.valueOf(field.getType());
        if (type.contains(" ")) {
            type = type.split(" ")[1];
        }
        switch (type) {
            case "java.lang.String": {
                val = value;
                break;
            }
            case "java.lang.Integer":
            case "int": {
                val = Integer.parseInt(value);
                break;
            }
            case "java.lang.Long":
            case "long": {
                val = Long.parseLong(value);
                break;
            }
            case "java.lang.Float":
            case "float": {
                val = Float.parseFloat(value);
                break;
            }
            case "java.lang.Double":
            case "double": {
                val = Double.parseDouble(value);
                break;
            }
            case "java.lang.Boolean":
            case "boolean": {
                val = Boolean.parseBoolean(value);
            }
        }
        return val;
    }
}
